package logic

import (
	"context"
	"database/sql"
	"fmt"

	"go-pod/pod-rpc/internal/svc"
	"go-pod/pod-rpc/model"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePodcastLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreatePodcastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePodcastLogic {
	return &CreatePodcastLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreatePodcast 创建播客
func (l *CreatePodcastLogic) CreatePodcast(in *pod.CreatePodcastReq) (*pod.CreatePodcastResp, error) {
	// 创建播客记录
	podcast := &model.Podcasts{
		Title: sql.NullString{
			String: in.Title,
			Valid:  len(in.Title) > 0,
		},
		Content: sql.NullString{
			String: in.Content,
			Valid:  len(in.Content) > 0,
		},
		Author: sql.NullString{
			String: in.Author,
			Valid:  len(in.Author) > 0,
		},
		Tags: sql.NullString{
			String: in.Tags,
			Valid:  len(in.Tags) > 0,
		},
	}

	// 插入数据库
	_, err := l.svcCtx.PodcastModel.Insert(l.ctx, podcast)
	if err != nil {
		return nil, fmt.Errorf("创建播客失败: %w", err)
	}

	return &pod.CreatePodcastResp{}, nil
}
